package adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.DecimalFormat;
import java.util.List;

import bean.AutoTestPaperBean;
import student.andlei.com.student.R;

public class PaperStructureAdapter extends BaseQuickAdapter<AutoTestPaperBean, BaseViewHolder> {
    private TextView textView;
    private double totalScore;
    public PaperStructureAdapter(int layoutResId, @Nullable List<AutoTestPaperBean> data, TextView textView) {
        super(layoutResId, data);
        this.textView = textView;
    }

    @Override
    protected void convert(BaseViewHolder helper, final AutoTestPaperBean item) {
        helper.setText(R.id.tv_type_name, item.type_name);
        final List<AutoTestPaperBean.mlist> singleQuestionList = item.list;
        if (singleQuestionList != null) {
            helper.setText(R.id.tv_type_number, "共" + singleQuestionList.size() + "题");
        }

        EditText editText = helper.getView(R.id.et_input_type_score);
        if (editText.getTag() != null && editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                try {
                    double typeScore = Double.parseDouble(editable.toString());
                    item.typeScore = typeScore;
                    totalScore = 0;
                    for (AutoTestPaperBean paperBean : getData()) {
                        totalScore = totalScore + paperBean.typeScore;
                    }
                    if (textView != null) {
                        textView.setText(totalScore + "分");
                    }
                    DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                    String percentScore = df.format(typeScore / singleQuestionList.size());
                    for (AutoTestPaperBean.mlist singleQuestionBean : singleQuestionList) {
                        singleQuestionBean.score =(percentScore);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        };
        editText.addTextChangedListener(textWatcher);
        editText.setTag(textWatcher);

        if (helper.getLayoutPosition() == 0) {
            helper.getView(R.id.iv_up).setVisibility(View.INVISIBLE);
            helper.getView(R.id.iv_down).setVisibility(View.VISIBLE);
        } else if (helper.getLayoutPosition() == getItemCount() - 1) {
            helper.getView(R.id.iv_up).setVisibility(View.VISIBLE);
            helper.getView(R.id.iv_down).setVisibility(View.GONE);
        }
        if(getData().size()<=1){
            helper.getView(R.id.iv_down).setVisibility(View.GONE);
        }
        helper.addOnClickListener(R.id.iv_up);
        helper.addOnClickListener(R.id.iv_down);
    }
}
